package com.zhihuichongcheng.application.ui.coordination.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.smartlib.cmnObject.util.FileUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.im.db.FileBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/11/8.
 */

public class DownLoadAdater extends RecyclerView.Adapter<DownLoadAdater.ViewHold> {
    private Context mContext;
    private List<FileBean> mListData = new ArrayList<>();
    public DownLoadAdater(Context context)
    {
        this.mContext = context;
    }
    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_download,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(final ViewHold holder, int position) {
        final FileBean bean = mListData.get(position);
        String path = bean.getPath();
        File file  = new File(path);
        if(file.length()== bean.getFilesize())
        {
            holder.mTvDownloadTitle.setText(bean.getName());
            holder.mBar.setVisibility(View.GONE);
            holder.mTvDownloadSize.setText(FileUtil.getFormatSize(bean.getFilesize())+"    完成");
        }
        else
        {
            holder.mTvDownloadTitle.setText(bean.getName());
            holder.mTvDownloadSize.setText(FileUtil.getFormatSize(file.length())+"/"+FileUtil.getFormatSize(bean.getFilesize()));
            holder.mTvDownloadProgress.setText((int)((file.length())*100/bean.getFilesize())+"%");

        }
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setData(List<FileBean> fileBeanList) {
        this.mListData = fileBeanList;
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private TextView mTvDownloadTitle,mTvDownloadSize,mTvDownloadProgress;
        private ProgressBar mBar;
        private ImageView mImAvatar;
        public ViewHold(View itemView) {
            super(itemView);
            mTvDownloadTitle = (TextView)itemView.findViewById(R.id.tv_download_title);
            mTvDownloadSize = (TextView)itemView.findViewById(R.id.tv_download_size);
            mTvDownloadProgress = (TextView)itemView.findViewById(R.id.tv_download_progress);
            mBar = (ProgressBar)itemView.findViewById(R.id.progress_bar);
            mImAvatar = (ImageView)itemView.findViewById(R.id.im_download_type);
        }
    }
}
